package source

import (
	"github.com/gin-gonic/gin"
	"smartLearn/models"
	"smartLearn/modules/response"
	"strconv"
	"strings"
)


func GetById(c *gin.Context) {
	sourceId,err:=strconv.Atoi(c.Query("id"))
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	sourceModel := models.SystemSource{};
	sourceModel.Id=sourceId
	sourceModel,err=sourceModel.GetById(sourceId)

	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}

	sourceLevelModel:= models.SystemSourceLevel{}
	levelIdsArr,err:=sourceLevelModel.GetBySourceId(sourceId)
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	var data = make(map[string]interface{}, 0)
	data["name"]=sourceModel.Name
	data["comment"]=sourceModel.Comment
	data["subjectId"]=sourceModel.SubjectId
	data["levelIds"]=strings.Join(levelIdsArr, ",")
	response.ShowData(c,data)
}
func GetList(c *gin.Context) {
	subjectId,err:=strconv.Atoi(c.Query("subjectId"))
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	//读取相关的阶段信息
	levelIds:=c.Query("levelIds")
	/*
	levelIdsArr := make([]int, len(levelIds))
	for i, v := range levelIds {
		levelIdsArr[i], err = strconv.Atoi(v)
		if err != nil {
			continue //如果传入非数组，那么忽略
		}
	}*/
	sourceModel := models.SystemSource{};
	clientsourceList,err :=sourceModel.GetBySubjectAndLevel(subjectId,levelIds)
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	var data = make(map[string]interface{}, 0)
	data["list"]=clientsourceList
	response.ShowData(c,data)

}
func Add(c *gin.Context) {
	name:=c.Query("name")
	comment:=c.Query("comment")
	subjectId,err:=strconv.Atoi(c.Query("subjectId"))
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	//读取相关的阶段信息
	levelIds:=strings.Split(c.Query("levelIds"),",")

	levelIdsArr := make([]int, len(levelIds))


	for i, v := range levelIds {
		levelIdsArr[i], err = strconv.Atoi(v)
		if err != nil {
			continue //如果传入非数组，那么忽略
		}
	}

	sourceModel := models.SystemSource{};
	sourceModel.Name=name
	sourceModel.Comment=comment
	sourceModel.SubjectId=subjectId
	_,err=sourceModel.Add(levelIdsArr)
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	response.ShowSuccess(c,"添加题目来源成功" )

}

func Update(c *gin.Context) {
	id,err:=strconv.Atoi(c.Query("id"))
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	name:=c.Query("name")
	comment:=c.Query("comment")
	subjectId,err:=strconv.Atoi(c.Query("subjectId"))
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	//读取相关的阶段信息
	levelIds:=strings.Split(c.Query("levelIds"),",")

	levelIdsArr := make([]int, len(levelIds))


	for i, v := range levelIds {
		levelIdsArr[i], err = strconv.Atoi(v)
		if err != nil {
			continue //如果传入非数组，那么忽略
		}
	}

	sourceModel := models.SystemSource{};
	sourceModel.Id=id
	sourceModel.Name=name
	sourceModel.Comment=comment
	sourceModel.SubjectId=subjectId
	_,err=sourceModel.Update(levelIdsArr)
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	response.ShowSuccess(c,"更新题目来源成功" )

}
func Delete(c *gin.Context) {

	sourceModel := models.SystemSource{};

	var err error
	sourceModel.Id,err =strconv.Atoi(c.Query("id"))
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}

	err=sourceModel.Delete()
	if err!=nil{
		response.ShowError(c,err.Error())
		return
	}
	response.ShowSuccess(c,"删除题目来源成功" )

}
